package com.fzy.modules.goods.service;

import com.fzy.core.common.IDUtils;
import com.fzy.core.common.UploadData;

import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

@Service("picService")
public class PicService {
	public UploadData uploadImage(MultipartFile upfile) {
		if(upfile!=null){
			//取原始文件名的后缀名
			///String oldName=upfile.getOriginalFilename();
			//生成新的文件名
			String filename=IDUtils.genImageName();//+oldName.substring(oldName.indexOf("."));
		
			String paths=System.getProperty("sso.webapp");
			//paths=paths.substring(0, paths.lastIndexOf(File.separator));
			String realPath=paths+"WEB-INF/pics";
			File dir = new File(realPath,filename);       
	        if(!dir.exists()){  
	            dir.mkdirs();  
	        }
			//spring自带上传文件功能
				try {
					upfile.transferTo(dir);//图片名字//提示标题//图片路径//图片大小//图片类型//上传是否成功
					return UploadData.getUploadData(null, "成功提示", "pics"+File.separator+filename, upfile.getSize(), null, "200");
				} catch (Exception e) {
					e.printStackTrace();
					return UploadData.getUploadData(null, "错误提示", null, 0, null, "500");
				} 
			}
		return UploadData.getUploadData(null, "错误提示", null, 0, null, "404!");

	}
		


}
